package com.simen.kv.sample;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.ListView;

import com.simen.absadapter.AbsAdapter;
import com.simen.kv.Columns;
import com.simen.kv.KVManager;
import com.simen.kv.RunSqlListener;
import com.simen.kv.KVUtils;

import java.util.ArrayList;
import java.util.List;

public class SortActivity extends Activity {

    private static final String TAG = "SortActivity";

    private EditText sortKey;
    private ListView listView;
    private AbsAdapter mAdapter;

    private List<String> results = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sort);

        sortKey = (EditText) findViewById(R.id.sortKey);
        listView = (ListView) findViewById(R.id.listView);

        mAdapter = new AbsAdapter<String>(this, results, android.R.layout.simple_list_item_1) {
            @Override
            protected boolean onItemAdapter(AbsViewHolder viewHolder, String element, int
                    position) {
                viewHolder.setTextOrButton(android.R.id.text1, element);
                return false;
            }
        };
        listView.setAdapter(mAdapter);

        sortKey.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String value = sortKey.getText().toString();
                results.clear();

                Log.d(TAG, "afterTextChanged: " + value);
                if (!"".equals(value)) {
                    KVUtils.runSql("select * from " + Constants.SORT_DB + " where " +
                            Columns.VALUENAME + " like '%" + value + "%'", null, new
                            RunSqlListener() {
                                @Override
                                public void onResult(Cursor cursor) {
                                    if (cursor != null) {
                                        while (cursor.moveToNext()) {
                                            String result = cursor.getString(cursor.getColumnIndex
                                                    (Columns
                                                            .VALUENAME));
                                            results.add(result);
                                        }
                                    }
                                }
                            });
                }

                mAdapter.notifyDataSetChanged();
            }
        });

        sampleData();
    }

    private void sampleData() {
        KVManager.getInstance().update(Constants.SORT_DB, "1", "Alaska");
        KVManager.getInstance().update(Constants.SORT_DB, "2", "Albany");
        KVManager.getInstance().update(Constants.SORT_DB, "3", "Annapolis");
        KVManager.getInstance().update(Constants.SORT_DB, "4", "Atlanta");
        KVManager.getInstance().update(Constants.SORT_DB, "5", "Augusta");
        KVManager.getInstance().update(Constants.SORT_DB, "6", "Austin");
        KVManager.getInstance().update(Constants.SORT_DB, "7", "Baton Rouge");
        KVManager.getInstance().update(Constants.SORT_DB, "8", "Bismarck");
        KVManager.getInstance().update(Constants.SORT_DB, "9", "Boise");
        KVManager.getInstance().update(Constants.SORT_DB, "10", "Boston");
        KVManager.getInstance().update(Constants.SORT_DB, "11", "Carson City");
        KVManager.getInstance().update(Constants.SORT_DB, "12", "Charleston");
        KVManager.getInstance().update(Constants.SORT_DB, "13", "Cheyenne");
        KVManager.getInstance().update(Constants.SORT_DB, "14", "Chicago");
        KVManager.getInstance().update(Constants.SORT_DB, "15", "Cincinati");
        KVManager.getInstance().update(Constants.SORT_DB, "16", "Cleveland");
        KVManager.getInstance().update(Constants.SORT_DB, "17", "Columbia");
        KVManager.getInstance().update(Constants.SORT_DB, "18", "Columbus");
        KVManager.getInstance().update(Constants.SORT_DB, "19", "Concord");
        KVManager.getInstance().update(Constants.SORT_DB, "20", "Dallas");
        KVManager.getInstance().update(Constants.SORT_DB, "21", "Denver");
        KVManager.getInstance().update(Constants.SORT_DB, "22", "Des Moines");
        KVManager.getInstance().update(Constants.SORT_DB, "23", "Detroit");
        KVManager.getInstance().update(Constants.SORT_DB, "24", "Dover");
        KVManager.getInstance().update(Constants.SORT_DB, "25", "Frankfort");
        KVManager.getInstance().update(Constants.SORT_DB, "26", "Harrisburg");
        KVManager.getInstance().update(Constants.SORT_DB, "27", "Hartford");
        KVManager.getInstance().update(Constants.SORT_DB, "28", "Hawaii");
        KVManager.getInstance().update(Constants.SORT_DB, "29", "Helena");
        KVManager.getInstance().update(Constants.SORT_DB, "30", "Houston");
        KVManager.getInstance().update(Constants.SORT_DB, "31", "Indianapolis");
        KVManager.getInstance().update(Constants.SORT_DB, "32", "Jackson");
        KVManager.getInstance().update(Constants.SORT_DB, "33", "Jefferson City");
        KVManager.getInstance().update(Constants.SORT_DB, "34", "Kansas City");
        KVManager.getInstance().update(Constants.SORT_DB, "35", "Lansing");
        KVManager.getInstance().update(Constants.SORT_DB, "36", "Lincoln");
        KVManager.getInstance().update(Constants.SORT_DB, "37", "Little Rock");
        KVManager.getInstance().update(Constants.SORT_DB, "38", "Los Angeles");
        KVManager.getInstance().update(Constants.SORT_DB, "39", "Madison");
        KVManager.getInstance().update(Constants.SORT_DB, "40", "Memphis");
        KVManager.getInstance().update(Constants.SORT_DB, "41", "Miami");
        KVManager.getInstance().update(Constants.SORT_DB, "42", "Minneapolis");
        KVManager.getInstance().update(Constants.SORT_DB, "43", "Montgomery");
        KVManager.getInstance().update(Constants.SORT_DB, "44", "Montpelier");
        KVManager.getInstance().update(Constants.SORT_DB, "45", "Morristown");
        KVManager.getInstance().update(Constants.SORT_DB, "46", "Nashville");
        KVManager.getInstance().update(Constants.SORT_DB, "47", "New Orleans");
        KVManager.getInstance().update(Constants.SORT_DB, "48", "New York");
        KVManager.getInstance().update(Constants.SORT_DB, "49", "Oklahoma City");
        KVManager.getInstance().update(Constants.SORT_DB, "50", "Olympia");
        KVManager.getInstance().update(Constants.SORT_DB, "51", "Philadelphia");
        KVManager.getInstance().update(Constants.SORT_DB, "52", "Phoenix");
        KVManager.getInstance().update(Constants.SORT_DB, "53", "Portland");
        KVManager.getInstance().update(Constants.SORT_DB, "54", "Providence");
        KVManager.getInstance().update(Constants.SORT_DB, "55", "Raleigh");
        KVManager.getInstance().update(Constants.SORT_DB, "56", "Richmond");
        KVManager.getInstance().update(Constants.SORT_DB, "57", "Sacramento");
        KVManager.getInstance().update(Constants.SORT_DB, "58", "Salem");
        KVManager.getInstance().update(Constants.SORT_DB, "59", "Salt Lake City");
        KVManager.getInstance().update(Constants.SORT_DB, "60", "San Francisco");
        KVManager.getInstance().update(Constants.SORT_DB, "61", "Santa Fe");
        KVManager.getInstance().update(Constants.SORT_DB, "62", "Seattle");
        KVManager.getInstance().update(Constants.SORT_DB, "63", "Springfield");
        KVManager.getInstance().update(Constants.SORT_DB, "64", "St Louis");
        KVManager.getInstance().update(Constants.SORT_DB, "65", "St Paul");
        KVManager.getInstance().update(Constants.SORT_DB, "66", "Tallahassee");
        KVManager.getInstance().update(Constants.SORT_DB, "67", "Topeka");
        KVManager.getInstance().update(Constants.SORT_DB, "68", "Trenton");
        KVManager.getInstance().update(Constants.SORT_DB, "69", "Washington");
    }
}
